﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WPF数据采集系统.MesClass;
using WPF数据采集系统.Model;

namespace WPF数据采集系统.View
{
    /// <summary>
    /// RotateMesView.xaml 的交互逻辑
    /// </summary>
    public partial class RotateMesView : UserControl
    {
        public RotateMesView()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ComboBox cmb = mes_combo;
            TextBox tb = cmb.Template.FindName("mes_url", cmb) as TextBox;
            string url = tb.Text;

            string sql1 = "Delete MesParams where url='" + url + "'";
            Tools.SqlHelper.Update(sql1);

            //DataTable dt = MainWindow.mvm.MesTable;
            //foreach (DataRow dr in dt.Rows)
            //{
            //    string sql = "insert into MesParams(parameter_name,parameter_type,parameter_value,url,isAvailable) values(@name,@type,@value,@url,@real)";
            //    SqlParameter[] param1 = new SqlParameter[]
            //    {
            //        new SqlParameter("@name",dr["parameter_name"]),
            //        new SqlParameter("@type",dr["parameter_type"]),
            //        new SqlParameter("@value",dr["parameter_value"]),
            //        new SqlParameter("@real",dr["isAvailable"]),
            //        new SqlParameter("@url",url)
            //    };
            //    Tools.SqlHelper.Update(sql, param1);
            //}
            LoadDataTable(url);
            //Tools.SqlHelper.SqlBulkCopyInsert("MesParams", MainWindow.mvm.MesTable);
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ComboBox cmb = mes_combo;
            TextBox tb = cmb.Template.FindName("mes_url", cmb) as TextBox;
            string url = tb.Text;
            string reslut = "";
            int reslut_int = HttpHelper.HttpGet(url, out reslut);
            richTextBox1.Document.Blocks.Clear();
            richTextBox1.AppendText(reslut);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            mes_combo.Items.Add("https://www.baidu.com");
            mes_combo.Items.Add("https://www.baidu1.com");
        }

        private void ComboBox_Prescription_TextChanged(object sender, RoutedEventArgs e)
        {
            ComboBox cmb = mes_combo;
            TextBox tb = cmb.Template.FindName("mes_url", cmb) as TextBox;
            string url = tb.Text;
            LoadDataTable(url);
        }

        private void ReceivedMessage(string name, string type, string value)
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                SynchronizationContext.SetSynchronizationContext(new
                    DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
                SynchronizationContext.Current.Post(pl =>
                {
                    //MainWindow.mvm.MesList.Add(new LogModel { RowNumber = MainWindow.mvm.MesList.Count + 1, DeviceName = name, LogInfo = type, Message = value, LogType = Base.LogType.Info });
                    //DataRow dr = MainWindow.mvm.MesTable.NewRow();
                    //dr["id"] = MainWindow.mvm.MesTable.Rows.Count + 1;
                    //dr["parameter_name"] = name;
                    //dr["parameter_type"] = type;
                    //dr["parameter_value"] = value;
                    //dr["isAvailable"] = 1;
                    //MainWindow.mvm.MesTable.Rows.Add(dr);

                }, null);
            });
        }
        public void LoadDataTable(string url)
        {
            //MainWindow.mvm.MesTable.Clear();
            //string sql = "select * from MesParams where url='" + url + "'";
            //DataTable dt = Tools.SqlHelper.GetDataSet(sql).Tables[0];
            //for (int i = 0; i < dt.Rows.Count; i++)
            //{
            //    dt.Rows[i]["id"] = i + 1;
            //}
            //MainWindow.mvm.MesTable = dt;
            //dataGrid1.ItemsSource = dt.DefaultView;
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            //ItemAddWindow iaw = new ItemAddWindow();
            //iaw.sendMessage += ReceivedMessage;
            //iaw.Show();
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            //删除用户
            DataRowView selectItem = dataGrid1.SelectedItem as DataRowView;
            if (selectItem == null) return;
            int id = (int)selectItem.Row["id"];

            ComboBox cmb = mes_combo;
            TextBox tb = cmb.Template.FindName("mes_url", cmb) as TextBox;
            string url = tb.Text;

            //DataTable dt = MainWindow.mvm.MesTable;
            //foreach (DataRow dr in dt.Rows)
            //{
            //    if (dr["id"].ToString() == id.ToString().Trim())
            //    {
            //        MainWindow.mvm.MesTable.Rows.Remove(dr);
            //        break;
            //    }
            //}
        }

        private void Edit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Disable_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Enable_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            SetDeviceStatus sds = new SetDeviceStatus();
            sds.param = new SetDeviceStatusParam();
            sds.action = "set_device_status";
            sds.param.device_code = "ROTATE";
            sds.param.status = "RUN";
            sds.param.errcode = "1020";
            sds.param.msg = "";
            string result = string.Empty;
            string sendData = JsonConvert.SerializeObject(sds);
            int re = HttpHelper.HttpPost("http://8.sctmes.com:38000", sendData, out result);
            if(re==0)
            {
                ResultSetDeviceStatus rsds = new ResultSetDeviceStatus();
                rsds = JsonConvert.DeserializeObject<ResultSetDeviceStatus>(result);
            }
            else
            {
                richTextBox1.Document.Blocks.Clear();
                richTextBox1.AppendText("发生异常:"+result);
            }
        }
    }
}
